package com.minsf.rpc.serializer;

import com.minsf.rpc.spi.SpiLoader;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 序列化器工厂
 * @Author minify
 * @Date 2024/3/26 19:51
 * @Project sf-rpc
 */
public class SerializerFactory {

    /*private final static Map<String, Serializer> KEY_SERIALIZER_MAP = new HashMap<String, Serializer>() {{
        put(SerializerKeys.JDK, new JdkSerializer());
        put(SerializerKeys.KRYO, new KryoSerializer());
        put(SerializerKeys.JSON, new JsonSerializer());
        put(SerializerKeys.HESSIAN, new HessianSerializer());
    }};*/

    static {
        // 工厂类加载时就加载序列化器
        SpiLoader.load(Serializer.class);
    }


    private final static Serializer DEFAULT_SERIALIZER = new JdkSerializer();

    /**
     * 获取序列化器，否则返回默认的序列化器
     * @param key
     * @return
     */
    public static Serializer getSerializer(String key) {
        return SpiLoader.getInstance(Serializer.class, key);
        //return KEY_SERIALIZER_MAP.getOrDefault(key, DEFAULT_SERIALIZER);
    }
}
